//
//  DetailOtherCell.m
//  PetAPP
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "DetailOtherCell.h"
#import "ModelOfPost.h"
#import <YYWebImage.h>

@interface DetailOtherCell ()

@property (nonatomic, retain) UIView *viewOfUser;

@property (nonatomic, retain) UIView *viewOfMessage;

@end


@implementation DetailOtherCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViewsWithFrame:(CGRect){.size = frame.size}];
        
    }
    return self;
}

- (void)handleDataWithModel:(ModelOfPost *)model {
    
    UIImageView *viewOfUserface = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 40, 40)];
    [self.viewOfUser addSubview:viewOfUserface];
    [viewOfUserface release];
    viewOfUserface.yy_imageURL = [NSURL URLWithString:model.userface];
    viewOfUserface.layer.cornerRadius = viewOfUserface.frame.size.width / 2;
    viewOfUserface.layer.masksToBounds = YES;
    
    UILabel *labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(viewOfUserface.frame.origin.x + viewOfUserface.frame.size.width + 5, 5, 200, 20)];
    [self.viewOfUser addSubview:labelOfName];
    [labelOfName release];
    labelOfName.text = model.author;
    
    UILabel *labelOfAdress = [[UILabel alloc] initWithFrame:CGRectMake(viewOfUserface.frame.origin.x + viewOfUserface.frame.size.width + 5, labelOfName.frame.size.height + labelOfName.frame.origin.y + 2, 200, 18)];
    [self.viewOfUser addSubview:labelOfAdress];
    [labelOfAdress release];
    labelOfAdress.text = model.address;
    labelOfAdress.textColor = [UIColor grayColor];
    labelOfAdress.font = [UIFont systemFontOfSize:14];
    
    
    UILabel *labelOftime = [[UILabel alloc] init];
    [self.viewOfUser addSubview:labelOftime];
    [labelOftime release];
    
    /** 计算多久之前的帖子 */
    NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:model.dateline.intValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *strDate = [dateFormatter stringFromDate:tempDate];
    [dateFormatter setDateFormat:@"MM-dd"];
    NSString *str2Date = [dateFormatter stringFromDate:tempDate];
    [dateFormatter release];
    
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSince1970];
    float seconds = (int)time - model.dateline.doubleValue;
    float points = seconds / 60;
    if (points < 60) {
        labelOftime.text = [NSString stringWithFormat:@"%d分钟前", (int)points];
        
    }else if (points > 60 && points < 3600) {
        
        labelOftime.text = [NSString stringWithFormat:@"%@",strDate];
    }else {
        labelOftime.text = [NSString stringWithFormat:@"%@",str2Date];
    }
    
    CGRect dateText = [labelOftime.text boundingRectWithSize:CGSizeMake(0, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil];
    labelOftime.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - dateText.size.width - 10, 15, dateText.size.width, 20);
    labelOftime.font = [UIFont systemFontOfSize:15];
    
    
    UILabel *labelOfContent = [[UILabel alloc] init];
    [self.viewOfMessage addSubview:labelOfContent];
    [labelOfContent release];
    
    labelOfContent.numberOfLines = 0;
    labelOfContent.text = [model.message[0] objectForKey:@"content"];
    CGRect contentRect = [[model.message[0] objectForKey:@"content"] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 65, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]} context:nil];
    
    labelOfContent.frame = CGRectMake(55, 10, [UIScreen mainScreen].bounds.size.width - 65, contentRect.size.height);
    labelOfContent.font = [UIFont systemFontOfSize:20];
    
    self.viewOfMessage.frame = CGRectMake(0, 50 , [UIScreen mainScreen].bounds.size.width, contentRect.size.height + 20);
    
    UIView *garLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.viewOfMessage.frame.origin.y + self.viewOfMessage.frame.size.height + 10, [UIScreen mainScreen].bounds.size.width, 1)];
    [self addSubview:garLine];
    [garLine release];
    garLine.layer.borderWidth = 1;
    garLine.layer.borderColor = [UIColor grayColor].CGColor;
    garLine.alpha = 0.3;
    
}



- (void)createSubViewsWithFrame:(CGRect)frame{
    
    self.viewOfUser = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
    [self addSubview:self.viewOfUser];
    [_viewOfUser release];
    
    self.viewOfMessage = [[UIView alloc] init];
    [self addSubview:self.viewOfMessage];
    [_viewOfMessage release];
    
}

- (void)dealloc {
    [_viewOfUser release];
    [_viewOfMessage release];
    [super dealloc];

}



@end
